using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace ShadowGame
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        private Player player;
        private List<Brick> bricks;
        private Door door;
        private const int numOfBricks = 5;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            graphics.PreferredBackBufferWidth = 1152;
            graphics.PreferredBackBufferHeight = 864;
            graphics.ApplyChanges();

            bricks = new List<Brick>();
            /*for (int i = 0; i < numOfBricks; i++)
            {
                bricks.Add(new Brick(this));
            }*/
            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            BackgroundManager.Initialize(
                Content.Load<Texture2D>(@"Images\Background"),
                new Rectangle(0, 0, Window.ClientBounds.Width, Window.ClientBounds.Height));

            Camera.WorldRectangle = BackgroundManager.WorldRectangle;
            Camera.Position = new Vector2(0, 400);
            Camera.ViewPortWidth = Window.ClientBounds.Width;
            Camera.ViewPortHeight = Window.ClientBounds.Height;

            createMap();
            player = new Player(Content, bricks);
            door = new Door(this);
            door.WorldPosition = new Vector2(BackgroundManager.Width - 256, BackgroundManager.Height - 300);
            
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            BackgroundManager.Update(gameTime);
            player.Update(gameTime);

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend);

            BackgroundManager.Draw(spriteBatch);
            player.Draw(spriteBatch);
            
            foreach (Brick brick in bricks)
            {
                brick.Draw(spriteBatch);
            }
            door.Draw(spriteBatch);
            spriteBatch.End();

            base.Draw(gameTime);
        }

        public void createMap()
        {
            Brick startBrick = new Brick(this); startBrick.WorldPosition = new Vector2(0, BackgroundManager.Height / 2);
            Brick brick1 = new Brick(this); brick1.WorldPosition = new Vector2(150, BackgroundManager.Height / 2 + 150);
            Brick brick2 = new Brick(this); brick2.WorldPosition = new Vector2(300, BackgroundManager.Height / 2 + 300);
            Brick brick3 = new Brick(this); brick3.WorldPosition = new Vector2(450, BackgroundManager.Height / 2 + 450);
            Brick brick4 = new Brick(this); brick4.WorldPosition = new Vector2(150, BackgroundManager.Height / 2 - 150);
            Brick brick5 = new Brick(this); brick5.WorldPosition = new Vector2(brick4.WorldPosition.X + brick4.WorldRectangle.Width, brick4.WorldPosition.Y);
            Brick brick6 = new Brick(this); brick6.WorldPosition = new Vector2(brick5.WorldPosition.X + 150, brick5.WorldPosition.Y - 150);
            Brick brick7 = new Brick(this); brick7.WorldPosition = new Vector2(brick6.WorldPosition.X + brick6.WorldRectangle.Width, brick6.WorldPosition.Y);
            Brick brick8 = new Brick(this);
            Brick brick9 = new Brick(this);
            Brick brick10 = new Brick(this);
            Brick brick11= new Brick(this);
            Brick brick12= new Brick(this);

            bricks.Add(startBrick);
            bricks.Add(brick1); bricks.Add(brick2);
            bricks.Add(brick3); bricks.Add(brick4);
            bricks.Add(brick5); bricks.Add(brick6);
            bricks.Add(brick7);
        }
    }
}
